package com.netafull.analyze;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class AnalyzeXMLHandlerForCategory extends DefaultHandler {
	List<Category> list;
	Category category;
	String tag;

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		if (category != null) {
			String data = new String(ch, start, length);
			if (tag.equals("id")) {
				category.setId(Integer.parseInt(data));
			} else if (tag.equals("category_name")) {
				category.setCategory_name(data);
			} else if (tag.equals("title_count")) {
				//System.out.println("-------------------"+data);
				category.setTitle_count(Integer.parseInt(data));
			} else if (tag.equals("img_flag")){
				category.setImg_flag(Integer.parseInt(data));
			}
		}
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if (category != null && localName.equals("category")) {
			list.add(category);
			category = null;
		}
		tag = "";
	}

	/*
	 * 开始解析文档
	 */
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		list = new ArrayList<Category>();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if (localName.equals("category")) {
			category = new Category();
		}
		tag = localName;
	}

	public List<Category> getParsedData() {
		return list;
	}
}
